//
//  JKShopsDetailVC.m
//  chequanbao_ios
//
//  Created by 朱凯 on 2018/1/29.
//  Copyright © 2018年 com.itboye. All rights reserved.
//

#import "JKShopsDetailVC.h"

// vc
#import "ByUIPayVC.h"

// view
#import "JKViewShopsDetailHeader.h"
#import "JKViewProductDetailStoreDetail.h"

// model
#import "ByModelShopsDetail.h"

// api
#import "ByApiShops.h"

@interface JKShopsDetailVC () <BMKLocationServiceDelegate>

@property (nonatomic, strong) UIScrollView *scroll;
@property (nonatomic, strong) JKViewShopsDetailHeader *header;
@property (nonatomic, strong) JKViewProductDetailStoreDetail *storeView;


// data
@property (nonatomic, strong) ByModelShopsDetail *detailModel;

// 实时定位
@property (nonatomic, strong) BMKLocationService *locService;

@end

@implementation JKShopsDetailVC

#pragma mark - 生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"商家详情";
    
    [[self.view viewWithTag:888] removeFromSuperview];
    [self addColorLeftBackBarButtonItem];
    
    [self getDetail];
}
- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
    [self setNaviAttributeWithTranslucent:false stateBarLightContent:false];
    
    // 初始化BMKLocationService
    _locService = [[BMKLocationService alloc]init];
    _locService.delegate = self;
    // 启动LocationService
    [_locService startUserLocationService];
}

- (void)viewWillDisappear:(BOOL)animated {
    
    [super viewWillDisappear:animated];
    
    _locService.delegate = nil;
    _locService = nil;
}
#pragma mark - 定位处理
//处理位置坐标更新
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation {
    NSLog(@"正在定位：%@", userLocation.location);
    self.header.location = userLocation.location;
}


#pragma mark - 请求数据
- (void)getDetail {
    
    ByNetParamsModel *params = [ByNetParamsModel new];
    
    params.seller_id = self.seller_id ? : [NSString stringWithFormat:@"%@", self.shopsItemModel.id];
    

    [ByCoreToolAlert showLoadind:@"正在加载..."];
    [ByApiShops getShopsDetail:params success:^(ByNetBaseResult *result) {
        
        [ByCoreToolAlert hideHud];
        
        if (self.shopsItemModel) {
            self.detailModel = result.data;
            self.detailModel.shopsItemModel = self.shopsItemModel;
        } else {
            self.detailModel = result.data;
        }
        
        
        [self createViews];
        self.header.model = self.detailModel;
        self.storeView.dic = self.detailModel.shopsItemModel.detail;
        
    } failure:^(ByNetBaseResult *result) {
        
        [ByCoreToolAlert showFailureWithText:result.msg];
        [self dismissViewControllerAnimated:true completion:nil];
    }];
}

#pragma mark - 创建视图
- (void)createViews {
    
    _header = [JKViewShopsDetailHeader new];
    [self.scroll addSubview:_header];
    [_header mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(@10);
        make.width.equalTo(@(SCREEN_WIDTH - 20));
        
    }];
    
    _storeView = [JKViewProductDetailStoreDetail new];
    [self.scroll addSubview:_storeView];
    [_storeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.width.equalTo(_header);
        make.top.equalTo(_header.mas_bottom).offset(10);
        
        
        make.bottom.equalTo(@-10);
    }];
}




#pragma mark - 懒加载
- (UIScrollView *)scroll {
    
    if (!_scroll) {
        _scroll = [UIScrollView new];
        _scroll.backgroundColor = TABLE_COLOR;
        
        [self.view addSubview:_scroll];
        
        [_scroll mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.equalTo(@0);
            make.width.equalTo(@(SCREEN_WIDTH));
            if (@available(ios 11.0,*)) {
                make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom);
            } else {
                make.bottom.equalTo(@0);
            }
        }];
    }
    return _scroll;
}

@end
